/**
 * @project_name staff_vehicle
 * @package_name com.ssh.dao
 * @desc TODO
 * @param
 * @author jack
 * @date 2013-4-25
 * @time 上午7:19:12
 */
package com.ssh.dao;

import java.util.List;

import org.hibernate.SessionFactory;

/**
 * @author jack
 *
 */
public class ObjectDAOImpl implements ObjectDAOI {
	private SessionFactory sessionFactory;

	/* (non-Javadoc)
	 * @see com.ssh.dao.ObjectDAOI#queryById(java.lang.String, java.lang.String)
	 */
	@Override
	public Object queryById(String id, String objectName) {
		// TODO Auto-generated method stub
		try{
			String hql = "from "+objectName+" as o where o.id=?" ;
			return sessionFactory.getCurrentSession().createQuery(hql).setString(0, id).uniqueResult();
		}
		catch(Exception e){
			return null;
		}


	}

	/* (non-Javadoc)
	 * @see com.ssh.dao.ObjectDAOI#queryByExtra(java.lang.String, java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Object> queryByParam(String param, String objectName) {
		// TODO Auto-generated method stub
		try{
			String hql = "from "+objectName+" as o where o."+param+"=?" ;
			return sessionFactory.getCurrentSession().createQuery(hql).setString(0, param).list();
		}
		catch(Exception e){
			return null;
		}
	}

	/* (non-Javadoc)
	 * @see com.ssh.dao.ObjectDAOI#deleteById(java.lang.String, java.lang.String)
	 */
	@Override
	public int deleteById(String id, String objectName) {
		// TODO Auto-generated method stub
		int num ;
		try{
			String hql = "delete "+objectName+" as o where o.id=?";
			num = sessionFactory.getCurrentSession().createQuery(hql).setString(0, hql).executeUpdate();
		}
		catch(Exception e){
			return 0 ;
		}
		return num;
	}

	/* (non-Javadoc)
	 * @see com.ssh.dao.ObjectDAOI#deleteByObject(java.lang.Object, java.lang.String)
	 */
	@Override
	public int deleteByObject(Object object, String objectName) {
		// TODO Auto-generated method stub
		try{
			sessionFactory.getCurrentSession().delete(object);
		}
		catch(Exception e){
			return 0 ;
		}
		return 1;
	}

	/* (non-Javadoc)
	 * @see com.ssh.dao.ObjectDAOI#deleteByObjectList(java.lang.Object, java.lang.String)
	 */
	@Override
	public int deleteObjectList(Object object, String objectName) {
		// TODO Auto-generated method stub
		return 0;
	}

	/* (non-Javadoc)
	 * @see com.ssh.dao.ObjectDAOI#updateByObject(java.lang.Object, java.lang.String)
	 */
	@Override
	public Object updateByObject(Object object, String objectName) {
		// TODO Auto-generated method stub
		try{
			sessionFactory.getCurrentSession().save(object);
		}
		catch(Exception e){
			return null;
		}

		return object;
	}

	/* (non-Javadoc)
	 * @see com.ssh.dao.ObjectDAOI#updateByObjectList(java.util.List, java.lang.String)
	 */
	@Override
	public int updateByObjectList(List<Object> object, String objectName) {
		// TODO Auto-generated method stub
		return 0;
	}

	/* (non-Javadoc)
	 * @see com.ssh.dao.ObjectDAOI#addObject(java.lang.Object, java.lang.String)
	 */
	@Override
	public Object addObject(Object object, String objectName) {
		// TODO Auto-generated method stub
		try{
			sessionFactory.getCurrentSession().save(object);
		}
		catch(Exception e){
			return null;
		}
		return object;
	}

	/* (non-Javadoc)
	 * @see com.ssh.dao.ObjectDAOI#addObjectList(java.util.List, java.lang.String)
	 */
	@Override
	public int addObjectList(List<Object> objectList, String objectName) {
		// TODO Auto-generated method stub
		return 0;
	}

	/* (non-Javadoc)
	 * @see com.ssh.dao.ObjectDAOI#countById(java.lang.String)
	 */
	@Override
	public int countById(String id) {
		// TODO Auto-generated method stub
		return 0;
	}

}
